/**
 * <p>文件名:		BlurObject.java</p>
 * <p>版权:		CopyrightTag</p>
 * <p>公司:		CompanyTag</p>
 * @author		周华彬(zhouhuabin@ctfo.com)
 */

package com.caits.lbs.framework.bean.common;

import java.io.Serializable;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * <p>
 * BlurObject
 * </p>
 * <p>
 * 模糊对象，不进行强制判断，只要能装换就转化，虽然使用时可能会出现类型不匹配的问题，但是会将类型装换可能带来的错误尽量缩小在一个类里面
 * </p>
 * 
 * @author 周华彬(zhouhuabin@ctfo.com)
 * @version 0.0.0
 *          <table style="border:1px solid gray;">
 *          <tr>
 *          <th width="100px">版本号</th><th width="100px">动作</th><th
 *          width="100px">修改人</th><th width="100px">修改时间</th>
 *          </tr>
 *          <!-- 以 Table 方式书写修改历史 -->
 *          <tr>
 *          <td>0.0.0</td>
 *          <td>创建类</td>
 *          <td>周华彬</td>
 *          <td>2008-7-11 下午04:00:09</td>
 *          </tr>
 *          <tr>
 *          <td>XXX</td>
 *          <td>XXX</td>
 *          <td>XXX</td>
 *          <td>XXX</td>
 *          </tr>
 *          </table>
 */
public class BlurObject implements Serializable, Cloneable {

	/** TODO */
	private static final long serialVersionUID = 1L;

	/** 对象 */
	public static final int OBJECT = 1;

	/** 模糊对象 */
	public static final int BLUR_OBJECT = 2;

	/** 整数 */
	public static final int INT = 3;

	/** 长整数 */
	public static final int LONG = 4;

	/** 串 */
	public static final int STRING = 5;

	/** 浮点 */
	public static final int FLOAT = 6;

	/** 双精度 */
	public static final int DOUBLE = 7;

	/** 布尔 */
	public static final int BOOLEAN = 8;

	/** 映射 */
	public static final int MAP = 9;

	/** 列表 */
	public static final int LIST = 10;

	/** 集合 */
	public static final int SET = 11;

	/** 对象值本身 */
	private Object obj;

	/** 类型，是支持的几种类型之一 */
	private int type;

	/** 如果是随意对象，那么需要提供类对象 */
	private Class<? extends Object> objClass;

	/**
	 * 构造器
	 */
	public BlurObject() {

	}

	/**
	 * 构造器
	 * 
	 * @param o
	 */
	public BlurObject(Object o) {
		obj = o;
		type = OBJECT;
		if (o != null) {
			objClass = o.getClass();
		}

	}

	/**
	 * 构造器
	 * 
	 * @param m
	 */
	public BlurObject(Map<Object,Object> m) {
		obj = m;
		type = MAP;
		objClass = Map.class;
	}

	/**
	 * 构造器
	 * 
	 * @param s
	 */
	public BlurObject(Set<?> s) {
		obj = s;
		type = SET;
		objClass = Set.class;
	}

	/**
	 * 构造器
	 * 
	 * @param l
	 */
	public BlurObject(List<?> l) {
		obj = l;
		type = LIST;
		objClass = List.class;
	}

	/**
	 * 构造器
	 * 
	 * @param bo
	 */
	public BlurObject(BlurObject bo) {
		obj = bo;
		type = BLUR_OBJECT;
		if (bo != null) {
			objClass = BlurObject.class;
		}
	}

	/**
	 * 构造器
	 * 
	 * @param i
	 */
	public BlurObject(int i) {
		obj = i;
		type = INT;
		objClass = Integer.class;
	}

	/**
	 * 构造器
	 * 
	 * @param s
	 */
	public BlurObject(String s) {
		obj = s;
		type = STRING;
		objClass = String.class;
	}

	/**
	 * 构造器
	 * 
	 * @param f
	 */
	public BlurObject(float f) {
		obj = f;
		type = FLOAT;
		objClass = Float.class;
	}

	/**
	 * 构造器
	 * 
	 * @param d
	 */
	public BlurObject(double d) {
		obj = d;
		type = DOUBLE;
		objClass = Double.class;
	}

	/**
	 * 构造器
	 * 
	 * @param l
	 */
	public BlurObject(long l) {
		obj = l;
		type = LONG;
		objClass = Long.class;
	}

	/**
	 * 构造器
	 * 
	 * @param o
	 * @param c
	 */
	public BlurObject(Object o, Class<? extends Object> c) {
		obj = o;
		type = OBJECT;
		objClass = c;
	}

	/**
	 * 构造器
	 * 
	 * @param b
	 */
	public BlurObject(boolean b) {
		obj = b;
		type = BOOLEAN;
		objClass = Boolean.class;
	}

	/**
	 * 获取数据类型
	 * 
	 * @return
	 */
	public int getType() {
		return type;
	}

	/**
	 * 输出对象
	 * 
	 * @return
	 */
	public Object asObject() {
		return obj;
	}

	/**
	 * 输出模糊对象
	 * 
	 * @return
	 */
	public BlurObject asBlurObject() {
		if (type == BLUR_OBJECT) {
			return (BlurObject) obj;
		}
		return new BlurObject(obj);
	}

	/**
	 * 输出为映射
	 * 
	 * @return
	 */
	public Map<?,?> asMap() {
		if (type == MAP) {
			return (Map<?,?>) obj;
		}
		return null;
	}

	/**
	 * 输出为列表
	 * 
	 * @return
	 */
	public List<?> asList() {
		if (type == LIST) {
			return (List<?>) obj;
		}
		return null;
	}

	/**
	 * 输出为集合
	 * 
	 * @return
	 */
	public Set<?> asSet() {
		if (type == SET) {
			return (Set<?>) obj;
		}
		return null;
	}

	/**
	 * 输出整数
	 * 
	 * @return
	 */
	public int asInt() {
		if (obj == null) {
			return 0;
		}
		if (type == INT) {
			return ((Integer) obj).intValue();
		}
		if (type == LONG) {
			return ((Long) obj).intValue();
		}
		if (type == FLOAT) {
			return ((Float) obj).intValue();
		}
		if (type == DOUBLE) {
			return ((Double) obj).intValue();
		}
		if (type == STRING || type == OBJECT) {
			int ret = 0;
			try {
				ret = new Double(Double.parseDouble(obj.toString())).intValue();
			} catch (NumberFormatException e) {
				ret = 0;
			}
			return ret;
		}
		if (type == BLUR_OBJECT) {
			return ((BlurObject) obj).asInt();
		}

		return 0;
	}

	/**
	 * 输出串
	 * 
	 * @return
	 */
	public String asString() {
		if (obj == null) {
			return null;
		}
		if (type == STRING || type == OBJECT) {
			return obj.toString();
		}
		if (type == BLUR_OBJECT) {
			return ((BlurObject) obj).asString();
		}
		if (type == INT) {
			return obj + "";
		}
		if (type == LONG) {
			return obj + "";
		}
		if (type == FLOAT) {
			return obj + "";
		}
		if (type == DOUBLE) {
			return obj + "";
		}
		if (type == BOOLEAN) {
			return obj + "";
		}

		return "";
	}

	/**
	 * 输出浮点数
	 * 
	 * @return
	 */
	public float asFloat() {
		if (obj == null) {
			return 0.0f;
		}
		if (type == INT) {
			return ((Integer) obj).floatValue();
		}
		if (type == LONG) {
			return ((Long) obj).floatValue();
		}
		if (type == FLOAT) {
			return ((Float) obj).floatValue();
		}
		if (type == DOUBLE) {
			return ((Double) obj).floatValue();
		}
		if (type == STRING || type == OBJECT) {
			float ret = 0.0f;
			try {
				ret = new Double(Double.parseDouble(obj.toString()))
						.floatValue();
			} catch (NumberFormatException e) {
				ret = 0.0f;
			}
			return ret;
		}
		if (type == BLUR_OBJECT) {
			return ((BlurObject) obj).asFloat();
		}

		return 0.0f;
	}

	/**
	 * 输出双精度
	 * 
	 * @return
	 */
	public double asDouble() {
		if (obj == null) {
			return 0.0;
		}
		if (type == INT) {
			return ((Integer) obj).doubleValue();
		}
		if (type == LONG) {
			return ((Long) obj).doubleValue();
		}
		if (type == FLOAT) {
			return ((Float) obj).doubleValue();
		}
		if (type == DOUBLE) {
			return ((Double) obj).doubleValue();
		}
		if (type == STRING || type == OBJECT) {
			double ret = 0.0;
			try {
				ret = new Double(Double.parseDouble(obj.toString()))
						.doubleValue();
			} catch (NumberFormatException e) {
				ret = 0.0;
			}
			return ret;
		}
		if (type == BLUR_OBJECT) {
			return ((BlurObject) obj).asDouble();
		}

		return 0.0;
	}

	/**
	 * 输出长整形
	 * 
	 * @return
	 */
	public long asLong() {
		if (obj == null) {
			return 0l;
		}
		if (type == INT) {
			return ((Integer) obj).longValue();
		}
		if (type == LONG) {
			return ((Long) obj).longValue();
		}
		if (type == FLOAT) {
			return ((Float) obj).longValue();
		}
		if (type == DOUBLE) {
			return ((Double) obj).longValue();
		}
		if (type == STRING || type == OBJECT) {
			long ret = 0l;
			try {
				ret = new Double(Double.parseDouble(obj.toString()))
						.longValue();
			} catch (NumberFormatException e) {
				ret = 0l;
			}
			return ret;
		}
		if (type == BLUR_OBJECT) {
			return ((BlurObject) obj).asLong();
		}
		return 0l;
	}

	/**
	 * 输出指定类的对象
	 * 
	 * @param objClass
	 *            指定类
	 * @return 如果对象不能转换成指定类返回null，指定类是null，返回null。
	 */
	public Object asObject(Class<? extends Object> objClass) {
		Object object = null;
		try {
			object = objClass.cast(obj);
		} catch (ClassCastException e) {
			object = null;
		}
		return object;
	}

	/**
	 * 获得对象类
	 * 
	 * @return
	 */
	public Class<? extends Object> getObjectClass() {
		if (objClass != null) {
			return objClass;
		} else {
			if (obj != null) {
				return obj.getClass();
			}
		}
		return null;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#hashCode()
	 */
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((obj == null) ? 0 : obj.hashCode());
		result = prime * result
				+ ((objClass == null) ? 0 : objClass.hashCode());
		result = prime * result + type;
		return result;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		BlurObject other = (BlurObject) obj;
		if (this.obj == null) {
			if (other.obj != null)
				return false;
		} else if (!this.obj.equals(other.obj))
			return false;
		if (objClass == null) {
			if (other.objClass != null)
				return false;
		} else if (!objClass.equals(other.objClass))
			return false;
		if (type != other.type)
			return false;
		return true;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#toString()
	 */
	public String toString() {
		if (obj != null) {
			return obj.toString();
		}
		return "";
	}

	/**
	 * FIXME
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		// FIXME 没有完成压力测试
	}

}
